from .custom_models import db, User


def create_tables():
    with db.engine.connect() as connection:
        with connection.begin():
            db.create_all()


def create_user(username, email):
    new_user = User(username=username, email=email)
    db.session.add(new_user)
    db.session.commit()


def get_user_by_username(username):
    return User.query.filter_by(username=username).first()


def update_user_email(username, new_email):
    user = get_user_by_username(username)
    if user:
        user.email = new_email
        db.session.commit()
        return True
    return False


def delete_user(username):
    user = get_user_by_username(username)
    if user:
        db.session.delete(user)
        db.session.commit()
        return True
    return False
